<?
ini_set('track_errors', true);

function formatBytes($bytes, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');
  
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
  
    $bytes /= pow(1024, $pow);
  
    return round($bytes, $precision) . ' ' . $units[$pow];
}

function check_file($filename) {
	$message = false;
	if (file_exists($filename))
	{
		$message = "File exists";
	}
	elseif (preg_match('/\.php$/', $filename))
	{
		$message = "Can not upload PHP files";
	}
	return $message;
}

$file = isset($_GET['url']) ? explode('/', $_GET['url']) : null;

if ($file && is_file($file[0]))
{
	if (isset($file[1]) && $file[1] == 'binary')
	{
		header('Content-Disposition: attachment; filename='.basename($file[0]));
		header('Content-Type: application/octet-stream');
	}
	elseif (isset($file[1]) && $file[1] == 'text')
	{
		header('Content-Type: text/plain');
	}
	else
	{
		header('Content-Type: ' . mime_content_type($file[0]));
	}
	header('Content-Description: File Transfer');
	header('Content-Transfer-Encoding: binary');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Pragma: public');
	header('Content-Length: ' . filesize($file[0]));
	ob_clean();
	flush();
	readfile($file[0]);
	exit;
}
else
{
	$message = "$_SERVER[REQUEST_METHOD] ";

	if ($_SERVER['REQUEST_METHOD'] == "PUT")
	{
		$filename = urldecode(basename($_SERVER['REQUEST_URI']));
		$message .= "$filename: ";
		$res = check_file($filename);
		if ($res)
		{
			$message .= $res;
		}
		else
		{
			if ($f = @fopen($filename, "w"))
			{
				if ($s = @fopen("php://input", "r"))
				{
					while($kb = fread($s, 1024)) { fwrite($f, $kb, 1024); }
					header("HTTP/1.1 201 Created");
					$message .= "OK";
					fclose($s);
				}
				else
				{
					$message .= $php_errormsg;
				}
				fclose($f);
			}
			else
			{
				$message .= $php_errormsg;
			}
		}
		echo "$message\n";
		exit;
	}
	elseif (isset($_FILES['userfile']))
	{
		$filename = $_FILES['userfile']['name'];
		$message .= "$filename: ";
		$res = check_file($filename);
		if ($res)
		{
			$message .= $res;
		}
		elseif (move_uploaded_file($_FILES['userfile']['tmp_name'],
			$filename))
		{
			$message .= "OK";
		}
		else
		{
			$message .= "Failed $php_errormsg";
		}
	}
	else
	{
		$message .= "index";
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Files</title>
	</head>
	<body>
	<h1>Files</h1>
		<div><?= $message ?></div>
		<form enctype="multipart/form-data" method="post" action="">
			<p>
				Max upload size <?= max(ini_get('post_max_size'), ini_get('upload_max_filesize')) ?>
				<input name="userfile" type="file"/>
				<input type="submit" value="Upload"/>
			</p>
			<ul>
<?
	if ($handle = opendir('.')) {
		while (false !== ($file = readdir($handle))) {
			if (preg_match('/^\.|php$/', $file) == 0) {
				$size = formatBytes(filesize($file));
				echo "<li><a href=\"$file/binary\">BIN</a> <a href=\"$file/text\">TXT</a> <a href=\"$file\">$file</a> ($size)</li>\n";
			}
		}
		closedir($handle);
	}
?>
			</ul>
		</form>
	</body>
</html>

<?
}
?>
